package com.sjw.workflow.dag;

import com.sjw.workflow.core.Agent;
import com.sjw.workflow.core.AgentInput;
import com.sjw.workflow.core.WorkflowContext;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;

/**
 * Workflow节点，表示DAG中的一个节点
 */
public class WorkflowNode {
    
    private final String id;
    private final String name;
    private final Agent agent;
    private final List<String> dependencies;
    private final LogicalExpression condition;
    private final Map<String, Object> nodeData;
    
    public WorkflowNode(String id, String name, Agent agent) {
        this.id = id;
        this.name = name;
        this.agent = agent;
        this.dependencies = new ArrayList<>();
        this.condition = null;
        this.nodeData = new HashMap<>();
    }
    
    public WorkflowNode(String id, String name, Agent agent, LogicalExpression condition) {
        this.id = id;
        this.name = name;
        this.agent = agent;
        this.dependencies = new ArrayList<>();
        this.condition = condition;
        this.nodeData = new HashMap<>();
    }
    
    /**
     * 添加依赖节点
     */
    public void addDependency(String nodeId) {
        if (!dependencies.contains(nodeId)) {
            dependencies.add(nodeId);
        }
    }
    
    /**
     * 检查节点是否准备就绪（所有依赖都满足且条件为真）
     */
    public boolean isReady(WorkflowContext context) {
        // 检查依赖是否都完成
        for (String depId : dependencies) {
            if (!context.isAgentCompleted(depId)) {
                return false;
            }
        }
        
        // 检查逻辑条件
        if (condition != null) {
            return condition.evaluate(context);
        }
        
        return true;
    }
    
    /**
     * 检查节点是否可以执行（依赖完成且条件满足）
     */
    public boolean canExecute(WorkflowContext context) {
        return isReady(context) && agent.isReady(context);
    }
    
    /**
     * 获取节点的输入数据
     */
    public AgentInput getInput(WorkflowContext context) {
        Map<String, Object> inputData = new HashMap<>();
        Map<String, Object> metadata = new HashMap<>();
        
        // 首先尝试从上下文中获取预设的输入数据
        String inputKey = id + "_input";
        Object presetInput = context.getGlobalData(inputKey);
        if (presetInput instanceof Map) {
            inputData.putAll((Map<String, Object>) presetInput);
        }
        
        // 从依赖节点收集输出数据
        for (String depId : dependencies) {
            if (context.isAgentSuccessful(depId)) {
                Map<String, Object> depOutput = context.getAgentResult(depId).getOutput();
                if (depOutput != null) {
                    inputData.putAll(depOutput);
                }
            }
        }
        
        // 添加节点特定的数据
        inputData.putAll(nodeData);
        
        // 添加元数据
        metadata.put("nodeId", id);
        metadata.put("nodeName", name);
        metadata.put("dependencies", dependencies);
        
        return new AgentInput(inputData, metadata);
    }
    
    // Getters
    public String getId() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    
    public Agent getAgent() {
        return agent;
    }
    
    public List<String> getDependencies() {
        return new ArrayList<>(dependencies);
    }
    
    public LogicalExpression getCondition() {
        return condition;
    }
    
    public Map<String, Object> getNodeData() {
        return new HashMap<>(nodeData);
    }
    
    public void setNodeData(String key, Object value) {
        nodeData.put(key, value);
    }
}
